﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Runda.CMS.Web.Controls;
using System.Xml;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using System.Web.Caching;

namespace Runda.CMS.Web.Controls.Lookup
{
    public abstract class LookupPageBase:AppPage
    {
        // Fields
        protected string _lookupClass;
        private int _maxRecordsReturned;
        protected string[] _objectTypes;
        protected bool _showNewButton;
        protected bool _showPropButton;

        // Properties
        public abstract LookupMode Mode
        {
            get;
            set;
        }

        public string[] ObjectTypes
        {
            get
            {
                return this._objectTypes;
            }
        }

        // Methods
        protected LookupPageBase()
        {
            this._maxRecordsReturned = 100;
        }

        protected override void ConfigurePage()
        {
            string str = base.Request.QueryString["Browse"];
            if (str == null)
            {
                str = "0";
            }
            this.Mode = (LookupMode)IntegerType.FromObject(ObjectType.BitOrObj(this.Mode, Interaction.IIf(StringType.StrCmp(str, "1", false) == 0, LookupMode.Browse, LookupMode.None)));
            this._lookupClass = base.Request.QueryString["Class"];
            this.ProcessSearchTypes();
        }

        private void ProcessSearchTypes()
        {
            this._showNewButton = false;
            this._showPropButton = false;
        }

        protected void RenderButtons()
        {
            base.Response.Write("<button disabled=\"true\" tabindex=\"8\" id=\"btnProperties\" onclick=\"showProperties();\" style=\"");
            if (!this._showPropButton)
            {
                base.Response.Write("display: none;");
            }
            base.Response.Write("\" title=\"");
            base.Response.Write("查看选中记录的属性");
            base.Response.Write("\">");
            base.Response.Write("属性");
            base.Response.Write("</button>");
            base.Response.Write("<span style=\"width: 5px;\"></span><button tabindex=\"9\" id=\"btnNew\" onclick=\"createNew();\" style=\"width: 95px;");
            if (!this._showNewButton)
            {
                base.Response.Write("display: none;\"");
            }
            else
            {
                base.Response.Write("\"");
            }
            base.Response.Write(" title=\"");
            base.Response.Write("新增一条记录");
            base.Response.Write("\">");
            base.Response.Write("新增");
            base.Response.Write("</button>");
        }
        protected void RenderCreatePrivileges()
        {
        }
        protected void RenderResultsFrame()
        {
            base.Response.Write("<iframe scrolling=\"no\" tabindex=\"4\" class=\"results\" onreadystatechange=\"resultsReady()\" id=\"frmResults\" src=\"map_lookupdata.aspx");
            base.Response.Write("?");
            base.Response.Write(base.Request.QueryString.ToString());
            base.Response.Write("&Mode=");
            base.Response.Write((int)this.Mode);
            if (!CheckBit((int)this.Mode, 1))
            {
                base.Response.Write("&ColumnsOnly=1");
            }
            base.Response.Write("\"></iframe>");
        }
        public static bool CheckBit(int val, int bit)
        {
            return ((bit & val) == bit);
        }
        protected void RenderSearchHeader()
        {
            base.Response.Write("<table width=\"100%\" cellpadding=\"2\" cellspacing=\"0\"><col><col width=\"100%\"><col>");
            base.Response.Write("<tr><td height=\"25\" noWrap><b style=\"color:#666666;\">");
            base.Response.Write("类型:");
            base.Response.Write("</b></td><td colspan=\"2\">");
            this.RenderSelectTypes();
            base.Response.Write("</td></tr>");
            if (!CheckBit((int)this.Mode, 1))
            {
                base.Response.Write("<tr><td noWrap><b style=\"color:#666666;\">");
                base.Response.Write("查找:");
                base.Response.Write("</b></td>");
                base.Response.Write("<td><input maxlength=\"100\" onkeydown=\"findValueKeyDown()\" tabindex=\"2\" type=\"text\" id=\"findValue\" style=\"width:100%;font-size:9pt;border:1px solid #7b9ebd;\"></td>");
                base.Response.Write("<td><button disabled=\"true\" tabindex=\"3\" id=\"btnGo\" onclick=\"search();\"");
                base.Response.Write(" title=\"");
                base.Response.Write("查找记录");
                base.Response.Write("\">");
                base.Response.Write("查找");
                base.Response.Write("</button></td></tr>");
            }
            base.Response.Write("<tr><td colspan=3 id=\"ErrorMessage\" style=\"padding-top:10px;display:none;\"><table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" style=\"background-color:#ffffcc;border:1px solid #919276;table-layout:fixed;padding-top:20px\"><col width=\"25\"><col><tr><td style=\"text-align:center;vertical-align:middle;\"><img src=\"/Images/16_info.gif\"></td><td style=\"padding:5px;\">");
            base.Response.Write(string.Format("超过 {0} 条可用记录。如果你没有发现你要找的记录，试试另一个条目。", this._maxRecordsReturned));
            base.Response.Write("</td></tr></table></td></tr><tr><td colspan=3 style=\"height:10px;font-size:1px;border-bottom:2px inset #ffffff;\">&nbsp;</td></tr></table>");
        }

        protected void RenderSelectTypes()
        {
            XmlDocument xmlDocument = GetXmlDocument("MapXml/" + this._lookupClass + ".xml");
            if ((xmlDocument != null) && (xmlDocument.DocumentElement.Attributes.GetNamedItem("name") != null))
            {
                base.Response.Write(base.Server.HtmlEncode(xmlDocument.DocumentElement.Attributes.GetNamedItem("name").Value));
            }
        }


        public static XmlDocument GetXmlDocument(string path)
        {
            XmlDocument document3;
            path = System.Web.HttpContext.Current.Server.MapPath(path);
            try
            {
                document3 = (XmlDocument)System.Web.HttpRuntime.Cache[path];
                if (document3 == null)
                {
                    document3 = new XmlDocument();
                    document3.Load(path);
                    System.Web.HttpRuntime.Cache.Insert(path, document3, new CacheDependency(path));
                }
            }
            catch
            {
                return null;
            }
            XmlDocument document2 = new XmlDocument();
            int num2 = document3.ChildNodes.Count - 1;
            for (int i = 0; i <= num2; i++)
            {
                document2.AppendChild(document2.ImportNode(document3.ChildNodes[i], true));
            }
            return document2;

        }
    }
}
